﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automobile.service.Model;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;

namespace Automobile.service.DAL
{
   public class WareHoseDal
   {
       string str = "Data Source=.;Initial Catalog=qichefuwu;Integrated Security=True";

       /// <summary>
       /// 显示库房信息
       /// </summary>
       /// <returns></returns>
       public List<WareHoseModel> WareHose()
       {
           string sql = "select WareHose_Id,WareHose_Name,WareHose_People,WareHose_Phone,WareHose_Address,WareHose_Remark from WareHose";
           SqlDataAdapter adapter = new SqlDataAdapter(sql, str);
           DataTable table = new DataTable();
           adapter.Fill(table);
           List<WareHoseModel> list = new List<WareHoseModel>();
           foreach (DataRow item in table.Rows)
           {
               WareHoseModel w = new WareHoseModel();
               w.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
               w.WareHose_Name = item["WareHose_Name"].ToString();
               w.WareHose_People = item["WareHose_People"].ToString();
               w.WareHose_Phone = item["WareHose_Phone"].ToString();
               w.WareHose_Address = item["WareHose_Address"].ToString();
               w.WareHose_Remark = item["WareHose_Remark"].ToString();
               list.Add(w);
           }
           return list;
       }
       /// <summary>
       /// 添加库房信息
       /// </summary>
       /// <returns></returns>
       public int AddWareHose(WareHoseModel w)
       {
           string sql = "insert into WareHose(WareHose_Name,WareHose_People,WareHose_Phone,WareHose_Address,WareHose_Remark) values ('"+w.WareHose_Name+"','"+w.WareHose_People+"','"+w.WareHose_Phone+"','"+w.WareHose_Address+"','"+w.WareHose_Remark+"')";
           SqlConnection conn = new SqlConnection(str);
           conn.Open();
           SqlCommand comm = new SqlCommand(sql, conn);
           int i = comm.ExecuteNonQuery();
           conn.Close();
           return i;
       }
       /// <summary>
       /// 删除库房信息
       /// </summary>
       /// <returns></returns>
       public int DeleWareHose(int WareHose_Id)
       {
           string sql = "delete from WareHose where WareHose_Id=" + WareHose_Id;
           SqlConnection conn = new SqlConnection(str);
           conn.Open();
           SqlCommand comm = new SqlCommand(sql, conn);
           int i = comm.ExecuteNonQuery();
           conn.Close();
           return i;
       }

       /// <summary>
       /// 取单个库房信息
       /// </summary>
       /// <returns></returns>
       public DataTable GetWareHose(string WareHose_Id)
       {
           string sql = "select  WareHose_Id,WareHose_Name,WareHose_People,WareHose_Phone,WareHose_Address,WareHose_Remark from WareHose where WareHose_Id=" + WareHose_Id;

           SqlConnection conn = new SqlConnection(str);
           conn.Open();//打开数据库

           SqlCommand comm = new SqlCommand(sql, conn);
           DataTable table = new DataTable();
           SqlDataAdapter ada = new SqlDataAdapter(comm);
           ada.Fill(table);
           conn.Close();
           return table;
       }

       /// <summary>
       /// 更新库房信息
       /// </summary>
       /// <param name="stu"></param>
       /// <returns></returns>
       public int UpdateWareHose(WareHoseModel w)
       {
           string sql = "update WareHose set WareHose_Name='" + w.WareHose_Name + "',WareHose_People='" + w.WareHose_People + "',WareHose_Phone='" + w.WareHose_Phone + "',WareHose_Address='" +w. WareHose_Address + "',WareHose_Remark='"+w.WareHose_Remark+"' where WareHose_Id='"+w.WareHose_Id+"'";

           SqlConnection conn = new SqlConnection(str);
           conn.Open();//打开数据库

           SqlCommand comm = new SqlCommand(sql, conn);
           //执行命令
           int count = comm.ExecuteNonQuery();
           conn.Close();

           return count;
       }
    }
}
